package com.mrjy.video.bo;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.HappinessInfo;
import com.mrjy.common.entity.IntegralInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.VideoCommentInfo;
import com.mrjy.common.entity.VideoInfo;
import com.mrjy.common.util.DateUtil;
import com.mrjy.common.util.ExcelUtil;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.integral.dao.IHappinessInfoDao;
import com.mrjy.integral.util.HappinessInfoEnum;
import com.mrjy.integral.util.HappinessInfoUtil;
import com.mrjy.integral.util.IntegralEnum;
import com.mrjy.integral.util.IntegralUtil;
import com.mrjy.video.dao.IVideoCommentInfoDao;
import com.mrjy.video.dao.IVideoInfoDao;
/**
 * 微课评论 -- 业务类
 * @author Javin
 */
@Service("videoCommentInfoBo")
public class VideoCommentInfoBo implements IVideoCommentInfoBo {
    @Resource
    private IVideoCommentInfoDao videoCommentInfoDao;
    @Resource
    private IVideoInfoDao videoInfoDao;
    @Resource
	private IHappinessInfoDao happinessInfoDao;

    @Override
    public Object txAddVideoCommentInfo(VideoCommentInfo videoComment,Map<String, Object> map) {
        videoComment.setUpdateDate(DateUtil.dateToString(new Date(), 2));
        if(StringUtils.isBlank(videoComment.getContent())){
            map.put("err_code", 2);
            map.put("err_msg", "【评论内容为空");
            return map;
        }

        int result = videoCommentInfoDao.insert(videoComment);
        if(result == 1){
            map.put("err_code", 0);
            map.put("err_msg", "评论成功");
            return map;
        }else{
            map.put("err_code", 1);
            map.put("err_msg", "评论失败");
            return map;
        }
    }

    @Override
    public Map<String, Object> videoCommentInfoList(VideoCommentInfo videoComment) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", videoCommentInfoDao.countVideoCommentInfo(videoComment));
        List<VideoCommentInfo> alist = videoCommentInfoDao.queryPageVideoCommentInfo(videoComment);
        map.put("rows", alist);
        return map;
    }

    @Override
    public VideoCommentInfo queryById(int id) {
        return videoCommentInfoDao.queryById(id);
    }

    @Override
    public void txUpdateById(VideoCommentInfo video) {
        videoCommentInfoDao.updateById(video);
    }

    @Override
    public int txDeleteByIds(int[] ids) {
        try {
            return videoCommentInfoDao.deleteByIds(ids);
        } catch (DataAccessException e) {
            return 0;
        }
    }

    @Override
    public Object updateDisableFlag(VideoCommentInfo video) {
        Map<String, Object> map = new HashMap<String, Object>();
        int count = videoCommentInfoDao.updateDisableFlag(video);
        if (count > 0) {
            map.put("err_code", 0);
            map.put("err_msg", "更新成功");
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "更新失败");
        }
        return map;
    }


    @Override
    public List<VideoCommentInfo> queryByVideoId(int videoId) {
        return videoCommentInfoDao.queryByVideoId(videoId);
    }

    @Override
    public Map<String,Object> queryPageByVideoId(VideoCommentInfo comment) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("total", videoCommentInfoDao.countByVideoId(comment.getVideoId()));
        map.put("rows",videoCommentInfoDao.queryPageByVideoId(comment));
        return map;
    }

    @Override
    public Object queryPageCommentsByVideoId(VideoCommentInfo comment, HttpSession session) {
        Map<String,Object> map = new HashMap<String,Object>();
        UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
//        if (null == loginUser) {
//            map.put("err_code", -1);
//            map.put("err_msg", "用户未登录");
//            return map;
//        }
        comment.setUserId(null == loginUser?null:loginUser.getId());
        int total = videoCommentInfoDao.queryCountCommentsByVideoId(comment);
        List<VideoCommentInfo> list = null;
        if (total > 0) {
            list = videoCommentInfoDao.queryPageCommentsByVideoId(comment);
            map.put("total", total);
            map.put("rows", list);
            map.put("err_code", 0);
        } else {
            map.put("err_code", 1);
            map.put("total", total);
            map.put("err_msg", "数据为空");
        }
        return map;
    }

    @Override
    public Object txtoComment(VideoCommentInfo vComment, HttpSession session) {
        Object existVideo;
        Map<String,Object> map = new HashMap<String, Object>();
        UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
        if (userInfo == null) {
            map.put("err_code", 1);
            map.put("err_msg", "没有用户信息");
            return map;
        }
        if(vComment == null){
            map.put("err_code", -1);
            map.put("err_msg", "传进来的对象为空");
            return map;
        }
        if(vComment.getVideoId() == null){
            map.put("err_code", -2);
            map.put("err_msg", "视频id为空");
            return map;
        }else{
            existVideo = videoInfoDao.queryById(vComment.getVideoId());
            if(existVideo==null){
                map.put("err_code", -3);
                map.put("err_msg", "通过VideoId查无此记录");
                return map;
            }
        }
        try {
            vComment.setContent(URLDecoder.decode(vComment.getContent(),"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        vComment.setDisableFlag(0);
        vComment.setUpdateDate(DateUtil.dateToString(new Date(), 2));
        vComment.setUserId(userInfo.getId());
        int count = videoCommentInfoDao.insert(vComment);
        if(count > 0){
            VideoInfo existVideoInfo = (VideoInfo) existVideo;
            VideoInfo videoInfo = new VideoInfo();
            videoInfo.setId(vComment.getVideoId());
            videoInfo.setCommentNum(existVideoInfo.getCommentNum() + 1);
            videoInfoDao.updateById(videoInfo);
            
            //评论回答的操作，新增积分
            IntegralInfo integral = IntegralUtil.setIntegral(vComment.getUserId()
    				, IntegralEnum.VIDEO_COMMIT.getScore()
    				, IntegralEnum.VIDEO_COMMIT.getTypeDesc()
    				, IntegralEnum.VIDEO_COMMIT.getTypeId()
    				, vComment.getId());
    		
    		//爱值添加
			HappinessInfo hi = HappinessInfoUtil.setHappinessInfo(vComment.getUserId()
					, HappinessInfoEnum.NOTE.getLoveValue(), HappinessInfoEnum.NOTE.getTypeDesc()
					, HappinessInfoEnum.NOTE.getTypeId(), "0", vComment.getId());
			happinessInfoDao.insert(hi);

            map.put("err_code", 0);
            map.put("err_msg", "评论成功");
            map.put("score", integral.getScore());
        }else{
            map.put("err_code", 1);
            map.put("err_msg", "评论不成功");
        }
        return map;
    }

    @Override
    public Object export(VideoCommentInfo videoComment, HttpSession session, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
        if (null == loginUser) {
            map.put("err_code", -1);
            map.put("err_msg", "用户未登录");
            return map;
        }
        videoComment.setUserId(null == loginUser ? null : loginUser.getId());


        List<VideoCommentInfo> dataset = videoCommentInfoDao.queryPageVideoCommentInfo(videoComment);
        if (dataset == null || dataset.size() == 0) {
            map.put("err_code", 404);
            map.put("err_msg", "无可导出数据");
            return map;
        }
        try {
            ExcelUtil.writeExcel(response, "留言" + DateUtil.dateToString(new Date(), DateUtil.FULL_DATE_STR) + ".xls", dataset, VideoCommentInfo.class);
            map.put("err_code", 200);
            map.put("err_msg", "导出成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("err_code", 500);
            map.put("err_msg", "导出失败");
        }
        return map;
    }


    @Override
    public Object queryVideoComments(VideoCommentInfo videoComment) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<VideoCommentInfo> list = videoCommentInfoDao.queryPageVideoCommentInfo(videoComment);

        if (list.size() > 0) {
            map.put("rows", list);
            map.put("err_code", 200);
            map.put("err_msg", "查询成功");
        } else {
            map.put("err_code", 500);
            map.put("err_msg", "查询失败");
        }

        return map;
    }

}
